from PySide6.QtWidgets import QMainWindow,QLabel
from sqlalchemy.orm import Session

from settings.initsetting.mode.initsetting import InitSetting
from settings.publicvar import engine
from views.home.home import Ui_home
from views.book.books.booksView import BooksView
from views.book.author.authorView import AuthorView
from views.book.category.categoryView import CategoryView
from views.book.publisher.publisherView import PublisherView
from views.schoolInfo.model.SchoolInfo import SchoolInfo
from views.schoolInfo.schoolinfoView import SchoolInfoView
from settings.initsetting.initsettingView import InitSettingView


class HomeView(QMainWindow,Ui_home):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.initLoadData()
        self.statusbar.addWidget(QLabel('  本程序由胡荣江独立开发，版权归胡荣江所有！'))

    '''
    放入的函数是菜单指向的函数
    '''
    def initLoadData(self):
        with Session(engine) as session,session.begin():
            try:
                session.query(InitSetting).one()
                schoolinfo = session.query(SchoolInfo).all()
                self.tool.hide()
                if not schoolinfo:
                    self.tool.setText('1、先添加学校信息。2、再添加初始化信息')
            except:
                self.tool.setText('1、先添加学校信息。2、再添加初始化信息')

    # 图书信息
    # 作者信息
    def authorWidget(self):
        self.setCentralWidget(AuthorView())

    # 图书详细信息
    def booksWidget(self):
        self.setCentralWidget(BooksView())

    # 图书分类信息
    def categoryWidget(self):
        self.setCentralWidget(CategoryView())

    # 出版社信息
    def publisherWidget(self):
        self.setCentralWidget(PublisherView())

    # 学校信息
    def schoolInfoWidget(self):
        self.setCentralWidget(SchoolInfoView())

    # 初始化信息
    def initsettingWidget(self):
        InitSettingView().exec()